﻿using System.ComponentModel;
using System.Runtime.CompilerServices;
using XamlFoundation.Annotations;

namespace XamlFoundation.Foundation
{
    public class ViewModelBase : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual bool ChangeAndNotify<T>(T value, ref T field, [CallerMemberName] string propertyName = null)
        {
            var hasChanged = Equals(value, field);

            if (hasChanged)
            {
                Notify(propertyName);    
            }
            
            return hasChanged;
        }

        [NotifyPropertyChangedInvocator]
        protected virtual void Notify([CallerMemberName] string propertyName = null)
        {
            var handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
